package com.hsc.www.authorizationServer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@SpringBootApplication
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class ResourceApplication {
    public static void main(String[] args) {
        SpringApplication.run(ResourceApplication.class, args);
    }

    @GetMapping("/user")
    @PreAuthorize("hasRole('Admin')")
    public Object getUser() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    @GetMapping("/dog")
    @PreAuthorize("hasRole('Admin') and #oauth2.hasScope('dog') ")
    public Object dog() {
        return  "myDog";
    }

    @GetMapping("/cat")
    @PreAuthorize("hasRole('Admin') and #oauth2.hasScope('cat') ")
    public Object cat() {
        return  "myCat";
    }


    /**
     *     public void configure(HttpSecurity http) throws Exception {
     *         http.formLogin()
     *                 .successHandler(appLoginInSuccessHandler)
     *                 .and()
     *                 .authorizeRequests().anyRequest().authenticated().and()
     *                 .csrf().disable().antMatcher("/oauth2/api/**").authorizeRequests()
     *                 .antMatchers( HttpMethod.GET,"/super/**").access("#oauth2.hasScope('read')")
     *                 .antMatchers( HttpMethod.GET,"/hello/**").access("#oauth2.hasScope('write')");
     *     }
     */





}
